(function() {

    "use strict";

    describe("DomTestCase", function() {

        var html = "<fieldset>" +
         "<input type=\"text\" name=\"message\" value=\"hello\" id=\"msg\">" +
         "<input type=\"text\" name=\"message\" value=\"\" id=\"msgEmpty\">" +
         "<input type=\"submit\" value=\"Send\">" +
         "</fieldset>";

        var element = document.createElement("form");
        element.innerHTML = html;

        var body = document.getElementsByTagName("body")[0];
        body.appendChild(element);

        beforeEach(function() {
            this.plugin = new nw.plugins.Dom();
        });

        it("should get value of element", function() {
            expect(this.plugin.getValue("#msg")).to.eql("hello");
            expect(this.plugin.getValue("#msg")).to.eql("hello");
        });

        it("should get value of first element", function() {
            expect(this.plugin.getValue("input")).to.eql("hello");
        });

        it("should set value of element", function() {
            this.plugin.setValue("#msgEmpty", "welcome");
            expect(this.plugin.getValue("#msgEmpty")).to.eql("welcome");
        });

        it("should set value for all elements", function() {
            this.plugin.setValue("input", "all elements");
            expect(this.plugin.getValue("#msg")).to.eql("all elements");
            expect(this.plugin.getValue("#msgEmpty")).to.eql("all elements");
        });

    });

	describe("DomHtmlGetSetTestCase", function() {

		var html = "<div id=\"panel\"><span>content</span></div>";

		var element = document.createElement("form");
		element.innerHTML = html;

		var body = document.getElementsByTagName("body")[0];
		body.appendChild(element);

		beforeEach(function() {
			this.plugin = new nw.plugins.Dom();
		});

		it("should get and set html", function() {
			expect(this.plugin.getHtml("#panel")).to.eql("<span>content</span>");
			this.plugin.setHtml("#panel", "<span>new content</span>");
			expect(this.plugin.getHtml("#panel")).to.eql("<span>new content</span>");
		});
	});

	describe("DomHtmlEventsTestCase", function() {

		var html = "<a src=\"javascript.void(0)\">click</a>";

		var element = document.createElement("form");
		element.innerHTML = html;

		var body = document.getElementsByTagName("body")[0];
		body.appendChild(element);

		beforeEach(function() {
			this.plugin = new nw.plugins.Dom();
		});

		it("should bind click event", function() {
			var handler = window.stubFn();
			this.plugin.bind("a", "click", handler);
			this.plugin.trigger("a", "click");
			expect(handler.called).to.be.ok();
		});

		it("should be multiple handler calls", function() {
			var handler = window.stubFn();
			this.plugin.bind("a", "click", handler);
			this.plugin.trigger("a", "click");
			expect(handler.called).to.be.ok();
			handler.called = false;
			this.plugin.trigger("a", "click");
			expect(handler.called).to.be.ok();
		});

		it("should unbind click event", function() {
			var handler = window.stubFn();
			this.plugin.bind("a", "click", handler);
			this.plugin.unbind("a", "click", handler);
			this.plugin.trigger("a", "click");
			expect(!handler.called).to.be.ok();
		});
	});

    describe("DomHtmlShowHideTestCase", function() {

        var html = "<div id=\"DomHtmlShowHideTestCase\"><span>content</span></div><div id=\"DomHtmlShowHideTestCase\"><span>content2</span></div>";

        var element = document.createElement("form");
        element.innerHTML = html;

        var body = document.getElementsByTagName("body")[0];
        body.appendChild(element);

        beforeEach(function() {
            this.plugin = new nw.plugins.Dom();
        });

        it("should get and set html", function() {
            expect(this.plugin.isVisible("#DomHtmlShowHideTestCase")).to.be.ok();
            this.plugin.hide("#DomHtmlShowHideTestCase");
            expect(!this.plugin.isVisible("#DomHtmlShowHideTestCase")).to.be.ok();
            this.plugin.show("#DomHtmlShowHideTestCase");
            expect(this.plugin.isVisible("#DomHtmlShowHideTestCase")).to.be.ok();
        });
    });
}());